package com.aaie.gru.theme.service.impl;

import com.aaie.gru.theme.entity.*;
import com.aaie.gru.theme.mapper.GruSysThemeConfigMapper;
import com.aaie.gru.theme.mapper.GruSysThemeFeedbackMapper;
import com.aaie.gru.theme.mapper.GruSysThemeMapper;
import com.aaie.gru.theme.service.IGruSysThemeFeedbackService;
import com.aaie.gru.util.CommonUtil;
import com.aaie.gru.util.ConstantUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lvxing
 * @since 2019-08-09
 */
@Service
public class GruSysThemeFeedbackServiceImpl extends ServiceImpl<GruSysThemeFeedbackMapper, GruSysThemeFeedback> implements IGruSysThemeFeedbackService {

    @Resource
    GruSysThemeFeedbackMapper gruSysThemeFeedbackMapper;

    @Resource
    GruSysThemeConfigMapper gruSysThemeConfigMapper;

    /*
    @Resource
    GruSysThemeMapper gruSysThemeMapper;*/

    @Override
    public IPage<GruSysThemeFeedbackVo> getGruSysThemeFeedbackVoList(GruSysThemeFeedbackDto param) {
        // 定义分页
        Page<GruSysThemeFeedbackVo> page = new Page<>((param.getCurrent() - 1) * param.getSize(), param.getSize());
        // 查询条件
        QueryWrapper<GruSysThemeFeedbackVo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotEmpty(param.getThemeId()), "f.theme_id", param.getThemeId());
        queryWrapper.eq(StringUtils.isNotEmpty(param.getThemeFeedbackReceiveId()), "f.theme_feedback_receive_id", param.getThemeFeedbackReceiveId());
        // 按更新时间倒叙排序
        queryWrapper.orderByDesc("f.update_time_sort");
        return gruSysThemeFeedbackMapper.selectGruSysThemeFeedbackVoList(page, queryWrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean themeFeedBackInfo(GruSysThemeFeedback param,String currentUserIdStr,String userTypeStr) {
        boolean resultValue = false;
        int insertThemeFeedBack = gruSysThemeFeedbackMapper.insert(param);
        if(insertThemeFeedBack != 0){
            GruSysThemeConfig gruSysThemeConfig = new GruSysThemeConfig();
            gruSysThemeConfig.setThemeType(ConstantUtil.THEME_TYPE_FEEDBACK);
            gruSysThemeConfig.setThemeNew(ConstantUtil.THEME_NEW_Y);
            gruSysThemeConfig.setThemeId(param.getThemeId());
            if(userTypeStr.equals(ConstantUtil.ROLE_NAME_GUARDIAN)){
                gruSysThemeConfig.setCurrentUserId(param.getThemeId());
            }else{
                gruSysThemeConfig.setCurrentUserId(param.getThemeFeedbackReceiveId());
            }


            try{
                QueryWrapper<GruSysThemeConfig> queryWrapper = new QueryWrapper();
                queryWrapper.eq("theme_id",param.getThemeId());
                queryWrapper.eq("theme_type",ConstantUtil.THEME_TYPE_FEEDBACK);
                if(userTypeStr.equals(ConstantUtil.ROLE_NAME_GUARDIAN)){
                    queryWrapper.eq("current_user_id",param.getThemeId());
                }else{
                    queryWrapper.eq("current_user_id",param.getThemeFeedbackReceiveId());
                }
                int count = gruSysThemeConfigMapper.selectCount(queryWrapper);
                if(count == 0){
                    gruSysThemeConfigMapper.insert(gruSysThemeConfig);
                }else{
                    GruSysThemeConfig gruSysThemeConfigUpdate = new GruSysThemeConfig();
                    gruSysThemeConfigUpdate.setThemeNew(ConstantUtil.THEME_NEW_Y);
                    UpdateWrapper<GruSysThemeConfig> updateWrapper = new UpdateWrapper<>();
                    updateWrapper.eq("theme_id",param.getThemeId());
                    updateWrapper.eq("theme_type",ConstantUtil.THEME_TYPE_FEEDBACK);
                    if(userTypeStr.equals(ConstantUtil.ROLE_NAME_GUARDIAN)){
                        updateWrapper.eq("current_user_id",param.getThemeId());
                    }else{
                        updateWrapper.eq("current_user_id",param.getThemeFeedbackReceiveId());
                    }

                    gruSysThemeConfigMapper.update(gruSysThemeConfigUpdate,updateWrapper);
                }

                resultValue = true;

            }catch (Exception ex){
                resultValue = false;
            }
        }

        return resultValue;
    }
}
